package com.jinyang.jetpackdemo.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.activity.ui.GlideUtil
import kotlinx.android.synthetic.main.activity_avatar_list.*

class AvatarListActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_avatar_list)
        val imageDatas: MutableList<String> = ArrayList()
        imageDatas.add("https://pic3.zhimg.com/v2-dc32dcddfd7e78e56cc4b6f689a24979_xl.jpg")
        imageDatas.add("https://pic1.zhimg.com/v2-c5f2fde6f6a00eba6cbeb66e8ddd421e_xl.jpg")
        imageDatas.add("https://pic2.zhimg.com/8ff6046c8d553baf14b1947cb75ad4d6_xl.jpg")
        imageDatas.add("https://pic2.zhimg.com/v2-fbfbfc97fee159541dd5a7ec4579a83c_xl.jpg")
        imageDatas.add("https://pic2.zhimg.com/v2-e75b5383a665819f29220b1df311998e_xl.jpg")
        imageDatas.add("https://pic1.zhimg.com/v2-ee443774322669f430a16ced71506101_xl.jpg")
        imageDatas.add("https://pic2.zhimg.com/v2-34b12353a31507e640b9f0fe78b1d770_xl.jpg")
        //加载本地资源
        avatarLayout1.setAvatarListListener(imageDatas)
        //顺序加载图片使用任意框架加载
        avatarLayout2.setAvatarListListener { imageViewList ->
            //创建的ImageView的数量
            val imageSize = imageViewList.size
            //实际需要显示的图片的数量
            val realDataSize = imageDatas.size
            val mul = imageSize - realDataSize
            for (i in 0 until imageSize) {
                if (i >= mul) { //6
                    //可以替换为网络图片处理
                    GlideUtil.loadImg(
                        this@AvatarListActivity,
                        imageDatas[realDataSize - (i - mul) - 1],
                        imageViewList[i]
                    )
                    imageViewList[i].visibility = View.VISIBLE
                } else {
                    imageViewList[i].visibility = View.GONE
                }
            }
        }
        //逆序加载图片使用任意框架加载
        avatarLayout3.setAvatarListListener { imageViewList ->
            //创建的ImageView的数量
            val imageSize = imageViewList.size
            //实际需要显示的图片的数量
            val realDataSize = imageDatas.size
            val mul = imageSize - realDataSize
            for (i in 0 until imageSize) {
                if (i >= mul) { //6
                    //可以替换为网络图片处理
                    GlideUtil.loadImg(
                        this@AvatarListActivity,
                        imageDatas[i - mul],
                        imageViewList[i]
                    )
                    imageViewList[i].visibility = View.VISIBLE
                } else {
                    imageViewList[i].visibility = View.GONE
                }
            }
        }
    }
}